﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Reflection;


namespace VSSAHT.DataAccess
{
    public class BaseDataAccess
    {

        #region Fields

        private string _connectionString = String.Empty;

        /// <summary>
        /// ConnectionString
        /// </summary>
        public string ConnectionString
        {
            get
            {
                return Security.Decrypt(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                //return ConfigurationManager.ConnectionStrings["VSS_AHT.Properties.Settings.ANHUNGTUONGConnectionString2"].ConnectionString);
            }
        }

        private string _providerName = String.Empty;
        /// <summary>
        /// ProviderName
        /// </summary>
        protected string ProviderName
        {
            get
            {
                if (_providerName == String.Empty)
                {
                    _providerName = ConfigurationManager.ConnectionStrings["ConnectionString"].ProviderName;
                }
                return _providerName;
            }
        }

        /// <summary>
        /// DbProviderFactory
        /// </summary>
        protected DbProviderFactory DbProviderFactory
        {
            get { return DbProviderFactories.GetFactory(ProviderName); }
        }

        protected string _errorMessage = String.Empty;
        /// <summary>
        /// Exception errorMessage
        /// </summary>
        public string ErrorMessage
        {
            get { return _errorMessage; }
        }

        #endregion

        #region Methods
        public bool CheckConnection()
        {
            bool isSuccess = false;
            // note: here you can use any persistance library
            // to dynamically populate a list of all objects
            try
            {
                // Process
                using (IDbConnection connection = DbProviderFactory.CreateConnection())
                {
                    connection.ConnectionString = ConnectionString;
                    //using (KimTrucSystemDataContext biocode = new KimTrucSystemDataContext(connection))
                    //{
                    //    isSuccess = biocode.DatabaseExists();
                    //}
                }
            }
            catch
            {
            }
            return isSuccess;
        }
        #endregion

        //#region
        //public bool CheckDatabases()
        //{
        //    bool result = false;
        //    using (VSSAHTDataContext dataContext = new VssTqhDataContext(base.ConnectionString))
        //    {
        //        if (dataContext.DatabaseExists())
        //            result = true;
        //        //dataContext.CreateDatabase();
        //    }
        //    return result;
        //}
        //#endregion
    }
}
